﻿@typeparam T

<MudStack>
    <MudTextField @bind-Value="_filterValue" Placeholder="Search" />
    <MudStack Row>
        <MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="@ApplyAsync" Class="apply-filter-button">Apply</MudButton>
        <MudButton Color="Color.Default" Variant="Variant.Filled" OnClick="@ResetAsync" Class="reset-filter-button">Reset</MudButton>
    </MudStack>
</MudStack>

@code {
    private string? _filterValue;

    [Parameter]
    public FilterContext<T>? FilterContext { get; set; }

    protected override void OnInitialized()
    {
        base.OnInitialized();

        if (FilterContext is not null)
        {
            FilterContext.FilterDefinition!.Operator = FilterOperator.String.Contains;

            if (FilterContext.FilterDefinition.Value is string stringValue)
            {
                _filterValue = stringValue;
            }
        }
    }

    private async Task ApplyAsync()
    {
        if (FilterContext is not null)
        {
            FilterContext.FilterDefinition!.Value = _filterValue;

            await FilterContext.Actions.ApplyFilterAsync(FilterContext.FilterDefinition);
        }
    }

    private async Task ResetAsync()
    {
        if (FilterContext is not null)
        {
            FilterContext.FilterDefinition!.Value = null;

            await FilterContext.Actions.ClearFilterAsync(FilterContext.FilterDefinition);
        }
    }
}
